package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.ServiceProviderPageQueryDTO;
import com.sky.entity.ServiceProvider;
import com.sky.enumeration.OperationType;
import com.sky.vo.ServiceProviderVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ServiceProviderMapper {

    /**
     * 根据分类id查询服务人员数量
     * @param categoryId
     */
    @Select("select count(id) from service_provider where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    @AutoFill(value = OperationType.INSERT)
    void insert(ServiceProvider serviceProvider);

    /**
     * 分页查询服务人员
     * @param serviceProviderPageQueryDTO
     */
    Page<ServiceProviderVO> pageQuery(ServiceProviderPageQueryDTO serviceProviderPageQueryDTO);

    /**
     * 根据主键查询服务人员
     * @param id
     */
    @Select("select * from service_provider where id=#{id}")
    ServiceProvider getById(Long id);

    /**
     * 删除服务人员
     * @param id
     */
    @Delete("delete from service_provider where id=#{id}")
    void deleteById(Long id);

    /**
     * 批量删除ids对应的服务人员
     * @param ids
     */
    void deleteByIds(List<Long> ids);

    /**
     * 根据服务人员id动态修改服务人员
     * @param serviceProvider
     */
    @AutoFill(value = OperationType.UPDATE)
    void update(ServiceProvider serviceProvider);

    /**
     * 根据服务人员id修改服务人员状态
     * @param serviceProvider
     */
    @Update("update service_provider set status=#{status} where id = #{id}")
    void updateStatus(ServiceProvider serviceProvider);

    /**
     * 动态条件查询服务人员
     * @param serviceProvider
     * @return
     */
    List<ServiceProvider> list(ServiceProvider serviceProvider);

    /**
     * 根据套餐id查询包含服务人员
     * @param id
     * @return
     */
    @Select("select sp.* from service_provider sp left join package_provider pp on sp.id = pp.provider_id where pp.package_id = #{packageId}")
    List<ServiceProvider> getByPackageId(Long id);

    /**
     * 根据手机号查询服务人员
     * @param phone
     * @return
     */
    @Select("select * from service_provider where phone = #{phone}")
    ServiceProvider getByPhone(String phone);

    /**
     * 根据用户名查询服务人员
     * @param username
     * @return
     */
    @Select("select * from service_provider where phone = #{username}")
    ServiceProvider getByUsername(String username);

    /**
     * 根据时间范围查询服务人员销量TOP10
     * @param begin
     * @param end
     * @return
     */
    @Select("select sp.name, count(od.id) as number from service_provider sp " +
            "left join order_detail od on sp.id = od.provider_id " +
            "left join orders o on od.order_id = o.id " +
            "where o.order_time between #{begin} and #{end} and o.status = 5 " +
            "group by sp.id, sp.name " +
            "order by number desc " +
            "limit 10")
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
} 